//
//  RegDoctorListViewController.m
//  安好
//
//  Created by lianchuang on 14-12-8.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "RegDoctorListViewController.h"
#import "RegDoctorCell.h"
#import "RegDoctorModal.h"
#import "FKSearchBar.h"
#import "RegDoctorDetialInfoViewController.h"

//doctoruid不为0的话就跳到这个页面
#import "DocInfoViewController.h"
//从百科医院-科室-过来的
#import "DepartmentViewController.h"
//从快速挂号过来--选好地区，医院，科室 后“确认挂号”过来
#import "QuickRegViewController.h"
//百科医院-》医生直接过来
#import "DoctorDetailViewController.h"
//分类挂号 -- 疾病
#import "IllnessViewController.h"

#pragma mark - RegHospitalList医院列表页面过来，期之前是从这三个页面过去的
//从特需挂号-》医院列表-》到当前医生列表
#import "TeXuRegViewController.h"
//分类挂号-》按医院 -》到当前医生列表
#import "ClassifyRegViewController.h"
//分类挂号-》按科室：选完科室->医院列表-》当前医生列表
#import "WikiDepartmentViewController.h"

#import "RegIllnessDocModal.h"


#define RGB(r, g, b)    [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]

#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

@interface RegDoctorListViewController ()<UITableViewDataSource,UITableViewDelegate,FKSearchBarDelegate>



//tab
@property(nonatomic,strong)UITableView * tableView;
//加载cell的数组
@property(nonatomic,strong)NSMutableArray * dataArray;
/**
 *  下拉加载页数
 */
@property (assign, nonatomic) NSInteger loadPage;

/**
 *  网络请求参数
 */
@property(nonatomic,strong)NSDictionary * requestParDic;

@property(nonatomic,strong)NSString *postUrl;

@end

@implementation RegDoctorListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
}

#pragma mark - 导航条设置
-(void)makeNavBar
{
    self.navigationItem.title = @"医生列表";
    //左按钮--返回
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
}
/**
 * 返回
 */
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadData
{
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    self.loadPage = 1;
    if ( [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[IllnessViewController class]]) {
        
        [self AFNetRequestForTuiJiangDoctorList];
    }else{
        
        [self AFNetRequestForDoctorList];

    }
   
}

-(void)AFNetRequestForTuiJiangDoctorList{
    
 [self showActionView];

    self.postUrl = [NSString stringWithFormat:@"%@symptom/recDoctor/",kBaseURL];
    //参数
    NSMutableDictionary * parDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [parDic setDictionary:self.regHosDepartDic];
    [parDic setValue:kPlatform forKey:@"platform"];
    [parDic setValue:kVersion forKey:@"version"];
    [parDic setValue:[NSString stringWithFormat:@"%d",self.loadPage] forKey:@"page"];
    self.requestParDic = parDic;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:self.postUrl parameters:self.requestParDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]){
            MyNSLog("%@",responseObject[@"msg"]);
            if (self.loadPage == 1 ) {
                [self shouLabel:@"暂无内容"];
            }
            [self.tableView footerEndRefreshing];
        }else{
            
            NSArray * doctorArray = responseObject[@"data"];
            if (doctorArray != nil)
            {
                self.loadPage++;
                for (NSDictionary * dic in doctorArray)
                {
                    RegIllnessDocModal * modal = [[RegIllnessDocModal alloc] initWithDic:dic];
                    [self.dataArray addObject:modal];
                }
                [self.tableView reloadData];
            }
            else
            {
                if (self.loadPage > 1)
                {
                    MyNSLog("上拉无数据");
                }
            }
            //结束刷新
            [self.tableView footerEndRefreshing];
        
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("网络失败");
        [self hiddenActionView];
        [self.tableView footerEndRefreshing];
    }];
    
    
    
}


-(void)AFNetRequestForDoctorList
{
    [self showActionView];
    /*
     //1从挂号-快速挂号-“确认挂号”过来的 ||2 //从百科-医院-科室-过来的|| //3分类挂号-》按医院 -》到当前医生列表 || 4 //特需- || 5//分类挂号-按科室 || 6 百科-医院-医生
     if(  [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[QuickRegViewController class]] || [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[ DepartmentViewController class]] ||[[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] isKindOfClass:[ClassifyRegViewController class]]|| [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] isKindOfClass:[TeXuRegViewController class]] ||  [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[ClassifyRegViewController class]] || [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-3] isKindOfClass:[WikiDepartmentViewController class]] || [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[DoctorDetailViewController class]]   )
     {
    
     }
     */
    self.postUrl = [NSString stringWithFormat:@"%@doctor/",kBaseURL];
    //参数
    NSMutableDictionary * parDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [parDic setDictionary:self.regHosDepartDic];
    [parDic setValue:kPlatform forKey:@"platform"];
    [parDic setValue:kVersion forKey:@"version"];
    [parDic setValue:@"1" forKey:@"guahao"];
    [parDic setValue:[NSString stringWithFormat:@"%d",self.loadPage] forKey:@"page"];
    self.requestParDic = parDic;
    
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:self.postUrl parameters:self.requestParDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]){
            MyNSLog("%@",responseObject[@"msg"]);
            if (self.loadPage == 1 ) {
                [self shouLabel:@"暂无内容"];
            }
            [self.tableView footerEndRefreshing];
        }
        else
        {
            NSArray * doctorArray = responseObject[@"data"];
            if (doctorArray != nil)
            {
                self.loadPage++;
                for (NSDictionary * dic in doctorArray)
                {
                    RegDoctorModal * modal = [[RegDoctorModal alloc] initWithDic:dic];
                    [self.dataArray addObject:modal];
                }
                [self.tableView reloadData];
            }
            else
            {
                if (self.loadPage > 1)
                {
                    MyNSLog("上拉无数据");
                }
            }
            //结束刷新
            [self.tableView footerEndRefreshing];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("网络失败");
        [self hiddenActionView];
        [self.tableView footerEndRefreshing];
    }];

}

-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight -iOS7TopMargin)style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView addFooterWithTarget:self action:@selector(AFNetRequestForDoctorList)];
    [self.tableView setSeparatorColor:RGB(221, 221, 221)];


}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (IPhone6Plus) {
        return 78;
    }else{
        return 65;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RegDoctorCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        if (IPhone6Plus) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"RegDoctorCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"RegDoctorCell" owner:self options:nil] firstObject];
        }
    }
    cell.docHeaderImg.clipsToBounds = YES;
    cell.docHeaderImg.layer.cornerRadius = 3;
    if ( [[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[IllnessViewController class]]) {
        
        RegIllnessDocModal * modal =self.dataArray[indexPath.row];
        [cell.docHeaderImg setImageWithURL:[NSURL URLWithString:modal.photourl] placeholderImage:[UIImage imageNamed:@"医生默认.png"]];
        cell.docNameLable.text = modal.doctorname;
        cell.docNameLable.width = 95;
        NSArray *subArr = [modal.feature componentsSeparatedByCharactersInSet:
                           [NSCharacterSet characterSetWithCharactersInString:@" "]];
        NSMutableString * goodStr = [NSMutableString stringWithCapacity:0];
        for (NSString * str in subArr) {
            [goodStr appendString:str];
        }
        
        cell.docGoodLable.text =goodStr;
        cell.docLevelNameLable.text = modal.doclevelname;
       
        if (modal.guahaoprice == nil||[modal.guahaoprice intValue] == 0 ) {
            cell.backView.hidden = YES;
        }else{
            cell.backView.hidden = NO;
            cell.regPriceLable.text = [NSString stringWithFormat:@"%d元",[modal.guahaoprice intValue]];
        }
        
        cell.anhaoIdent.hidden = YES;
        cell.anhaoIdent.backgroundColor =[UIColor colorWithRed:82/255.0 green:216/255.0 blue:184/255.0 alpha:1];
        cell.anhaoIdent.textColor = [UIColor whiteColor];
        cell.backView.backgroundColor=[UIColor colorWithRed:254/255.0 green:195/255.0 blue:10/255.0 alpha:1];
        
        cell.backView.frame = CGRectMake(kScreenWidth-(cell.backView.frame.size.width+15),(cell.contentView.height-cell.backView.height)/2.0 ,cell.backView.frame.size.width ,cell.backView.frame.size.height);
        cell.regPriceLable.textColor = [UIColor whiteColor];
        cell.regTitleLable.textColor = [UIColor whiteColor];
        
        cell.anhaoIdent.frame = CGRectMake(cell.docNameLable.right+10, 10, cell.anhaoIdent.frame.size.width, cell.anhaoIdent.frame.size.height);
        
        if (cell.backView.hidden) {
            cell.docGoodLable.width = kScreenWidth-15 - cell.docGoodLable.left;
        }else{
        cell.docGoodLable.frame = CGRectMake(cell.docGoodLable.origin.x, cell.docGoodLable.origin.y,cell.backView.left-cell.docGoodLable.origin.x-10, cell.docGoodLable.height);
        }
    
    
    }else{
        RegDoctorModal * modal = self.dataArray[indexPath.row];
        [cell.docHeaderImg setImageWithURL:[NSURL URLWithString:modal.photourl] placeholderImage:[UIImage imageNamed:@"医生默认.png"]];
        cell.docNameLable.text = modal.doctorname;
        cell.docNameLable.width = 95;
        //把空格切割掉
        NSArray *subArr = [modal.feature componentsSeparatedByCharactersInSet:
                           [NSCharacterSet characterSetWithCharactersInString:@" "]];
        NSMutableString * goodStr = [NSMutableString stringWithCapacity:0];
        for (NSString * str in subArr) {
            [goodStr appendString:str];
        }
        
        cell.docGoodLable.text =goodStr;
        cell.docLevelNameLable.text = modal.doclevelname;
        
        if (modal.price == nil || [modal.price intValue] == 0) {
            cell.backView.hidden = YES;
        }else{
            
            cell.regPriceLable.text = [NSString stringWithFormat:@"%d元",[modal.price intValue]];
        }
        
        
        if ([modal.doctoruid intValue] == 0) {
            cell.anhaoIdent.hidden = YES;
        }
        cell.anhaoIdent.backgroundColor =[UIColor colorWithRed:82/255.0 green:216/255.0 blue:184/255.0 alpha:1];
        cell.anhaoIdent.textColor = [UIColor whiteColor];
        cell.backView.backgroundColor=[UIColor colorWithRed:254/255.0 green:195/255.0 blue:10/255.0 alpha:1];
        
        cell.backView.frame = CGRectMake(kScreenWidth-(cell.backView.frame.size.width+15),(cell.contentView.height-cell.backView.height)/2.0 ,cell.backView.frame.size.width ,cell.backView.frame.size.height);
    
        cell.regPriceLable.textColor = [UIColor whiteColor];
        cell.regTitleLable.textColor = [UIColor whiteColor];
        
        cell.anhaoIdent.frame = CGRectMake(cell.docNameLable.right+10, 10, cell.anhaoIdent.frame.size.width, cell.anhaoIdent.frame.size.height);
        if (cell.backView.hidden) {
            cell.docGoodLable.width = kScreenWidth-15 - cell.docGoodLable.left;
        }else{
            cell.docGoodLable.frame = CGRectMake(cell.docGoodLable.origin.x, cell.docGoodLable.origin.y,cell.backView.left-cell.docGoodLable.origin.x-10, cell.docGoodLable.height);
        }
        
    }
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2] isKindOfClass:[IllnessViewController class]]) {
        
        RegIllnessDocModal * modal = self.dataArray[indexPath.row];
        RegDoctorDetialInfoViewController * docInfo =
        [[RegDoctorDetialInfoViewController alloc] init];
        docInfo.doctorid = modal.doctorid;
        
        [self.navigationController pushViewController:docInfo animated:YES];
    }
    else{
        RegDoctorModal * modal = self.dataArray[indexPath.row];
        RegDoctorDetialInfoViewController * docInfo =
        [[RegDoctorDetialInfoViewController alloc] init];
        docInfo.doctorid = modal.doctorid;
        [self.navigationController pushViewController:docInfo animated:YES];
    }

}


@end
